﻿Imports System.Data.OleDb


Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("FoodConnectionString").ConnectionString)
            Dim SqlString As String = "select * from Profile  where FoodUser=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then tb_FirstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("Description")) Then tb_Description.Text = dr("Description")
                If Not IsDBNull(dr("Address")) Then tb_Address.Text = dr("Address")

            End If
        End If

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SaveProfile.Click

        Dim newFileName As String = ""
        Dim SqlString As String
        If fup_Picture.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() + "_profile.jpg"
            Dim picLocationOnHD = Request.MapPath("pictures") & "/" & newFileName
            fup_Picture.SaveAs(picLocationOnHD)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("FoodConnectionString").ConnectionString)
        If fup_Picture.HasFile Then
            SqlString = "update profile set FirstName=@f1,LastName=@f2,Description=@f3,Picture=@f4, Address=@f5 where FoodUser=@f6"
        Else
            SqlString = "update profile set FirstName=@f1,LastName=@f2,Description=@f3, Address=@f5 where FoodUser=@f6"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
        If fup_Picture.HasFile Then
            cmd.Parameters.AddWithValue("@f4", "pictures/" & newFileName)
        End If
        cmd.Parameters.AddWithValue("@f5", tb_Address.Text)
        cmd.Parameters.AddWithValue("@f6", User.Identity.Name)

        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        Response.Redirect("~/Profile.aspx?FoodUser=" & User.Identity.Name)

    End Sub

End Class